package cn.prophet.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.prophet.bean.User;
import cn.prophet.bean.vo.SuperResponseBean;
import cn.prophet.service.LoginService;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("user")
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 注册
     * 
     * @param username
     * @param psw
     * @return
     */
    @GetMapping("registered")
    @ResponseBody
    public Map<String, Object> startRegistered(String username, String psw) {
        Integer status = loginService.startRegistered(username, psw);
        Map<String, Object> map = new HashMap<>();
        if (status != null && status == 1) {
            map.put("msg", "注册成功");
        } else {
            map.put("msg", "注册失败");
        }
        map.put("status", status);
        log.info("status:" + status);
        return map;
    }

    @GetMapping("login")
    @ResponseBody
    public SuperResponseBean<User> startLogin(String username, String psw) {
        User user = loginService.startLogin(username, psw);
        SuperResponseBean<User> responseBean = new SuperResponseBean<>();
        if (user != null) {
            responseBean.setCode(1);
            responseBean.setMsg("登录成功");
        } else {
            responseBean.setCode(0);
            responseBean.setMsg("登录失败");
        }
        responseBean.setData(user);
        return responseBean;
    }
    @GetMapping("resetPsw")
    @ResponseBody
    public Map<String,Object> resetPsw(@RequestParam(value = "userName",required = true) String userName,@RequestParam(value = "oldPsw",required = true)String oldPsw,@RequestParam(value = "newPsw",required = true)String newPsw){
        int flag = loginService.resetPsw(userName,oldPsw,newPsw);
        Map<String,Object> map = new HashMap<>();
        if(flag==1){
            map.put("code",1);
            map.put("msg","修改成功");
        }else if(flag==2){
            map.put("code",0);
            map.put("msg","用户名不存在");
        }else if(flag==3){
            map.put("code",0);
            map.put("msg","原始密码不正确");
        }else{
            map.put("code",0);
            map.put("msg","操作失败");
        }
        return map;
    }
    @GetMapping("newUserReset")
    @ResponseBody
    public Map<String,Object> newUserReset(@RequestParam(value = "userName",required = true)String userName,@RequestParam(value = "userPsw",required = true)String userPsw,@RequestParam(value = "withdraw_psw")String withdraw_psw){
        Map<String,Object> map = new HashMap<>(2);
        int result = loginService.newUserReset(userName,userPsw,withdraw_psw);

        if(result==1){
            map.put("code",1);
            map.put("msg","重置成功");
        }else if(result==2) {
            map.put("code",0);
            map.put("msg","资金密码长度不符合，必须6位");
        }else{
            map.put("code",0);
            map.put("msg","参数不能为空！");
        }

        return map;

    }
}
